<template>
  <div class="box">
    <div class="slide-left">
      <img :src="goodsObj.mainImage" alt />
    </div>
    <div class="slide-right">
      <h1>{{goodsObj.name}}</h1>
      <p>{{goodsObj.subtitle}}</p>
      <p>{{goodsObj.price}}元</p>
      <p>{{goodsObj.stock > 0 ? '有现货' : '暂无库存' }}</p>
      <button @click="addGoods">添加到购物车</button>
    </div>
  </div>
</template>
<script>
import axios from "axios";
import { mapMutations } from "vuex";
export default {
  name: "product",
  data() {
    return {
      goodsObj: {},
    };
  },
  created() {
    axios
      .get("/products/" + this.$route.params.id)
      .then((data) => (this.goodsObj = data))
      .catch((err) => window.console.log(err));
  },
  methods: {
    ...mapMutations(["getGoodsNum"]),
    addGoods() {
      window.console.log(this.goodsObj);
      axios
        .post("/carts", {
          productId: this.goodsObj.id,
          selected: true,
        })
        .then((data) => this.getGoodsNum(data.cartProductVoList.length))
        .catch((err) => window.console.log(err));
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../assets/scss/config.scss";
@import "../assets/scss/reset.scss";
.box {
  overflow: hidden;
  div {
    width: 50%;
  }
  .slide-left {
    float: left;
    img {
      width: 80%;
      margin-left: 20%;
    }
  }
  .slide-right {
    float: right;
    :nth-child(5) {
      button {
        width: 10%;
      }
    }
  }
}
</style>